package com.jz.beijianbao.legalperson.controller;


import com.jz.beijianbao.legalperson.bean.LegalPersonAdd;
import com.jz.beijianbao.legalperson.bean.LegalPersonUpdate;
import com.jz.beijianbao.legalperson.bean.LegalPersonUpdatestatus;
import com.jz.beijianbao.legalperson.bean.PlantSelectVo;
import com.jz.beijianbao.legalperson.service.LegalPersonService;
import com.jz.util.ResultBuildVO;
import com.jz.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


@RestController
@CrossOrigin
public class LegalPersonTest {

    @Autowired
    private LegalPersonService legalPersonService;

    /*//用户下拉框
    @GetMapping("user/ofCurrentUser")
    public ResultVO queryXiaLa(){
        ResultVO resultVO = legalPlantService.queryXiaLa();
        return resultVO;
    }*/

    //分页
    @PostMapping("user/query")
    public ResultVO querypage(PlantSelectVo plantSelectVo){
        if(plantSelectVo.getPageIndex()==null){
            plantSelectVo.setPageIndex(1);
        }
        if(plantSelectVo.getPageSize()==null){
            plantSelectVo.setPageSize(10);
        }
        ResultVO resultVO = legalPersonService.queryPage(plantSelectVo);
        return resultVO;
    }

    //新增
    @PostMapping("user/add")
    public ResultVO add(@RequestBody LegalPersonAdd legalPersonAdd){
        ResultVO resultVO = legalPersonService.add(legalPersonAdd);
        return resultVO;
    }

    //修改
    @PostMapping("user/update")
    public ResultVO update(@RequestBody @Valid LegalPersonUpdate legalPersonUpdate, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVO.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return legalPersonService.updatePlant(legalPersonUpdate);
    }

    //启用禁用
    @PostMapping("user/updatestatus")
    public ResultVO updateStatus(@RequestBody @Valid LegalPersonUpdatestatus legalPersonUpdatestatus, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuildVO.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return legalPersonService.updateStatus(legalPersonUpdatestatus);
    }
}